package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceImpl implements IPermissionService{
    @Autowired
    private PermissionMapper permissionMapper;

    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    public void insert(Permission permission) {
        permissionMapper.insert(permission);
    }

    public Permission select(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    public List<Permission> listAll() {
        return permissionMapper.selectAll();
    }

    public int update(Permission permission) {
        return permissionMapper.updateByPrimaryKey(permission);
    }

    @Override
    public PageInfo<Permission> query(QueryObject qo) {
        PageHelper.startPage(qo.getPageNum(),qo.getPageSize());
        List<Permission> result=permissionMapper.selectForList(qo);

        return new PageInfo(result);
    }

    @Autowired
    private ApplicationContext ctx;
   /* @Override
    public void reload() {
        List<String> expressions = permissionMapper.selectAllExpression();

        Map<String, Object> beans = ctx.getBeansWithAnnotation(Controller.class);
        Collection<Object> collections = beans.values();
        for (Object controller : collections) {
            //判断实例是否是代理类对象
            if (!AopUtils.isCglibProxy(controller)) {
                continue;
            }
            //拿到父类字节码对象
            Class clz = controller.getClass().getSuperclass();
            Method[] methods = clz.getDeclaredMethods();
            for (Method method : methods) {
                RequiresPermissions permission = method.getAnnotation(RequiresPermissions.class);
                if (permission!=null) {
                    String expression = permission.value()[0];
                    if (!expressions.contains(expression)) {
                        Permission permission1 = new Permission();
                        permission1.setName(permission.value()[1]);
                        permission1.setExpression(expression);
                        permissionMapper.insert(permission1);
                    }
                }
            }
        }
    }*/

    @Override
    public List<String> selectPermissionByEmpId(Long id) {
        return permissionMapper.selectPermissionByEmpId(id);
    }


}
